package com.aliengen.thingple.thingple.preferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class PreferencesUtil {
    private static SharedPreferences mSharedPreferences = null;
    private static SharedPreferences.Editor mEditor = null;

    /* Handheld preferences. */
    public final static String SHARE_CMCID = "cmcid";
    public final static String SHARE_UHF = "uhf_share";
    public final static String SHARE_BARCODE = "barcode_share";
    public final static String SHARE_FINGERPRINT = "fingerprint_share";
    public final static String SHARE_HF = "hf_share";
    public final static String SHARE_DEVICE = "device_share";
    public final static String SHARE_HAVE_SET = "have_set_share";

    /* General preferences. */
    public final static String APP_LANGUAGE = "language";

    /* Thingple API preferences. */
    public final static String SHARE_API_URL = "thingple_api_url";
    public final static String SHARE_API_CLIENT_ID = "thingple_api_client_id";
    public final static String SHARE_API_CLIENT_SECRET = "thingple_api_client_secret";

    public final static String SHARE_SINGLE_READ_POWER = "thingple_single_read_power";
    public final static String SHARE_INVENTORY_READ_POWER = "thingple_inventory_read_power";

    public static void init(Context context){
        if (null == mSharedPreferences) {
            mSharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context) ;
        }
    }

    public static void removeKey(String key){
        mEditor = mSharedPreferences.edit();
        mEditor.remove(key);
        mEditor.commit();
    }

    public static void removeAll(){
        mEditor = mSharedPreferences.edit();
        mEditor.clear();
        mEditor.commit();
    }

    public static String get(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    public static Boolean get(String key, boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    public static Integer get(String key, Integer defaultValue) {
        try {
            return Integer.valueOf(mSharedPreferences.getString(key, ""+defaultValue));
        } catch(ClassCastException e) {
            Log.e("PreferencesUtil", "Invalid integer value: " + mSharedPreferences.getString(key, ""+defaultValue));
            e.printStackTrace();
        }
        return defaultValue;
    }

    public static void set(String key, String value) {
        mEditor = mSharedPreferences.edit();
        mEditor.putString(key, value);
        mEditor.commit();
    }

    public static void set(String key, boolean value) {
        mEditor = mSharedPreferences.edit();
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    public static void set(String key, Integer value) {
        mEditor = mSharedPreferences.edit();
        mEditor.putInt(key, value);
        mEditor.commit();
    }
}
